package net.cjw.initsb.controller.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import springfox.documentation.annotations.ApiIgnore;
import net.cjw.initsb.dao.ManageUserMapper;
import net.cjw.initsb.entity.ManageUser;
@ApiIgnore
@RestController
public class RestTemplateTestController {
	@Autowired
	private ManageUserMapper userinfoMapper;
	@Autowired
	private RestTemplate restTemplate;

	@GetMapping(value = "rest1")
	public String test(@RequestParam String name) {
		System.out.println("rest1 name: " + name);
		ManageUser userinfo = userinfoMapper.selectByPrimaryKey("admin");
		if (userinfo == null) {
			return "找不到对象";
		}
		return userinfo.getNickname();
	}

	@PostMapping(value = "rest2")
	public String test2(@RequestParam String name) {
		System.out.println("rest2 name: " + name);

		String url = "http://localhost:9091/rest1?name=666";
		String json = restTemplate.getForEntity(url, String.class).getBody();
		return json;
	}

	@GetMapping("rest3")
	public String test3() {

		String url = "http://localhost:9091/rest2";
		MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
		param.add("name", "111");
		String json = restTemplate.postForEntity(url, param, String.class).getBody();
		return json;
	}
}
